package com.ihisun.tmhcms.core.service.doctorservice.impl;

import com.ihisun.tmhcms.core.dao.doctordao.EditDoctorDao;
import com.ihisun.tmhcms.core.dao.doctordao.EditDoctorMechanismDao;
import com.ihisun.tmhcms.core.dao.doctordao.EditDoctorPracticeDao;
import com.ihisun.tmhcms.core.dao.projectdao.EditProjectDao;
import com.ihisun.tmhcms.core.dto.doctordto.EditDoctorDto;
import com.ihisun.tmhcms.core.dto.doctordto.EditDoctorMechanismDto;
import com.ihisun.tmhcms.core.dto.projectdto.EditProjectDto;
import com.ihisun.tmhcms.core.service.doctormechanismservice.EditDoctorMechanismService;
import com.ihisun.tmhcms.core.service.doctorservice.EditDoctorService;
import com.ihisun.tmhcms.utils.DateUtils;
import com.ihisun.tmhcms.utils.ResponseVo;
import com.ihisun.tmhcms.utils.UuIdUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.beans.Transient;
import java.util.List;

@Service
public class EditDoctorServiceImpl implements EditDoctorService {
    @Resource
    EditDoctorMechanismService editDoctorMechanismService;
    @Resource
    EditDoctorMechanismDao editDoctorMechanismDao;
    @Resource
    EditDoctorDao editDoctorDao;
    @Resource
    EditDoctorPracticeDao editDoctorPracticeDao;
    @Resource
    EditProjectDao editProjectDao;

    @Override
    @Transactional
    public ResponseVo editDoctor(EditDoctorDto editDoctorDto) {
        if (editDoctorDto.getDoctorId() != null && !"".equals(editDoctorDto.getDoctorId())) {
            //修改
            /**
             * 修改医生主体信息
             */
            editDoctorDto.setUpdateTime(DateUtils.getCurrentTime());
            editDoctorDao.save(editDoctorDto);
            /**
             * 删除绑定的机构从新添加
             */
            editDoctorMechanismService.deleteByDocetorId(editDoctorDto.getDoctorId());

            /**
             * 删除医生执业证书
             */
            editDoctorPracticeDao.deleteByDoctorId(editDoctorDto.getDoctorId());
            /**
             * 添加新的执业信息
             */
            editDoctorDto.getDoctorPracticeDto().setDoctorId(editDoctorDto.getDoctorId());
            editDoctorDto.getDoctorPracticeDto().setCreateTime(DateUtils.getCurrentTime());
            editDoctorDto.getDoctorPracticeDto().setUpdateTime(DateUtils.getCurrentTime());
            editDoctorPracticeDao.save(editDoctorDto.getDoctorPracticeDto());

            /**
             *保存医生新绑定的机构
             */
            List<EditDoctorMechanismDto> doctorMechanism = editDoctorDto.getDoctorMechanism();
            for (int i = 0; i < doctorMechanism.size(); i++) {
                doctorMechanism.get(i).setDoctorId(editDoctorDto.getDoctorId());
                doctorMechanism.get(i).setUpdateTime(DateUtils.getCurrentTime());
                doctorMechanism.get(i).setCreateTime(DateUtils.getCurrentTime());
                editDoctorMechanismDao.save(editDoctorDto.getDoctorMechanism().get(i));
            }
            /**
             * 给医生绑定新的项目
             */
            if (editDoctorDto.getEditProjectDtos() != null && !editDoctorDto.getEditProjectDtos().isEmpty()) {
                List<EditProjectDto> editProjectDtos = editDoctorDto.getEditProjectDtos();
                for (int i = 0; i < editProjectDtos.size(); i++) {
                    editProjectDtos.get(i).setUpdateTime(DateUtils.getCurrentTime());
                    editProjectDtos.get(i).setCreateTime(DateUtils.getCurrentTime());
                    editProjectDtos.get(i).setDoctorId(editDoctorDto.getDoctorId());
                    editProjectDao.save(editProjectDtos.get(i));
                }
            }
        } else {
            String uuid = UuIdUtil.getToUuid();
            //添加
            /**
             * 添加机构主体信息
             */
            editDoctorDto.setDoctorId(uuid);
            editDoctorDto.setCreateTime(DateUtils.getCurrentTime());
            editDoctorDto.setUpdateTime(DateUtils.getCurrentTime());
            editDoctorDao.save(editDoctorDto);
            /**
             * 绑定机构
             */
            List<EditDoctorMechanismDto> doctorMechanism = editDoctorDto.getDoctorMechanism();
            for (int i = 0; i < doctorMechanism.size(); i++) {
                doctorMechanism.get(i).setDoctorId(uuid);
                doctorMechanism.get(i).setCreateTime(DateUtils.getCurrentTime());
                doctorMechanism.get(i).setUpdateTime(DateUtils.getCurrentTime());
                editDoctorMechanismDao.save(editDoctorDto.getDoctorMechanism().get(i));
            }
            /**
             *添加项目
             */
            List<EditProjectDto> editProjectDtos = editDoctorDto.getEditProjectDtos();
            for (int i = 0; i < editProjectDtos.size(); i++) {
                editProjectDtos.get(i).setDoctorId(uuid);
                editProjectDtos.get(i).setUpdateTime(DateUtils.getCurrentTime());
                editProjectDtos.get(i).setCreateTime(DateUtils.getCurrentTime());
                editProjectDao.save(editProjectDtos.get(i));
            }
            /**
             * 添加医生执业信息
             */
            editDoctorDto.getDoctorPracticeDto().setDoctorId(uuid);
            editDoctorDto.getDoctorPracticeDto().setCreateTime(DateUtils.getCurrentTime());
            editDoctorDto.getDoctorPracticeDto().setUpdateTime(DateUtils.getCurrentTime());
            editDoctorPracticeDao.save(editDoctorDto.getDoctorPracticeDto());
        }
        return ResponseVo.ok();
    }
}
